<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>待办事项</title>
    <style>
        ul {
            padding: 0;
        }
        ul li {
            margin-bottom: 1em;
        }
    </style>
</head>
<body>
    <input id='text' type="text">
    <input id='btn' type="button" value="添加" onclick='addItem()'>
    <ul id='item'>
        
    </ul>
</body>
<script>
    var text = document.getElementById('text');
    var btn = document.getElementById('btn');
    var item = document.getElementById('item');

    var arrItem = JSON.parse(localStorage.getItem('data'))

    arrItem.forEach(function(i) {
        //创建一个li
        var newLi = document.createElement('li');
        //给li设置值
        newLi.innerText = i;
        //将创建出来的li追加到ul中
        item.appendChild(newLi);
    })

    //添加事项
    function addItem(){
        //获取input中的值
        var r = text.value;
        arrItem.push(r)
        //创建一个li
        var newLi = document.createElement('li');
        //给li设置值
        newLi.innerText = r;
        //将创建出来的li追加到ul中
        item.appendChild(newLi);
        //将拿到的值保存到本地
        //1.localStorage
        // console.log(localStorage)
        //存 => 字符串
        localStorage.setItem('data',JSON.stringify(arrItem))
        //取
        console.log(localStorage.getItem('data'))
    }
</script>
</html>
